﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Danser_AJAXEnabledWebApplication.User_Controls;
using Danser_AJAXEnabledWebApplication.PageExt.Utilities;

namespace Danser_AJAXEnabledWebApplication.Query_Popups
{
    /// <summary>
    /// Pop up page for selecting anchor tests for comparisons
    /// </summary>
    public partial class SelectAnchors : System.Web.UI.Page
    {
        #region Constants:

        //Pattern for columns template
        private const String FIELD_PREFIX = "^";
        private const String FIELD_SUFFIX = "$";

        private const String COLUMNS_TEMPLATE =
            FIELD_PREFIX + "Name" + FIELD_SUFFIX + "|" +
            FIELD_PREFIX + "Date" + FIELD_SUFFIX + "|" +
            FIELD_PREFIX + "Time" + FIELD_SUFFIX + "|" +
            FIELD_PREFIX + "Operator" + FIELD_SUFFIX;

        public const String SESSION_SELECTED_ANCHORS = "SelectedAnchors";

        private const String ROW_SELECTOR_ID = "rowSelector";

        #endregion

        #region Members:

        /// <summary>
        /// handle formatting UI elements
        /// </summary>
        PageFormatter _pageFormatter = new PageFormatter();

        #endregion

        #region Page Load:

        /// <summary>
        /// event handler - initiations on page loading
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) // first time
            {
                IList<String> selectedAnchors = new List<String>();
                Session[SESSION_SELECTED_ANCHORS] = selectedAnchors;

                //filter grid properly - select only the anchors
                anchorsObjectDataSource.SelectParameters["filter"].DefaultValue = "isAnchor = 'true'";

                //display only columns defined in template
                _pageFormatter.DisplayColumnsTemplate(anchorsGridView, columnsMultiSelectList, 1, COLUMNS_TEMPLATE);
            }
        }

        #endregion

        #region Grid Handlers:

        /// <summary>
        /// event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rowSelector_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox selectedCheckBox = sender as CheckBox;
            GridViewRow row = (selectedCheckBox.Parent.Parent) as GridViewRow;

            //get the ids list from session
            IList<String> selectedAnchors = Session[SESSION_SELECTED_ANCHORS] as IList<String>;
            
            //get the id accoreding to selected row
            String testId = anchorsGridView.DataKeys[row.RowIndex].Value.ToString();

            //add id to list
            selectedAnchors.Add(testId);

        }

        /// <summary>
        /// event handler - filter grid's columns
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void filterButton_Click(object sender, EventArgs e)
        {
            _pageFormatter.FilterColumnsByListSelection(anchorsGridView, columnsMultiSelectList);
        }

        #endregion
    }
}
